<?php
/**
 * 后台Index相关
 */
namespace Admin\Controller;
use Think\Controller;
use Think\Exception;

/**
 * 文章内容管理
 */
class ProductController extends CommonController {

    public function index() {
        $conds = array();
        $title = $_GET['title'];
        if($title) {
            $conds['title'] = $title;
        }
        if($_GET['catid']) {
            $conds['catid'] = intval($_GET['catid']);
        }

        $page = $_REQUEST['p'] ? $_REQUEST['p'] : 1;
        $pageSize = 10;

        $product = D("Product")->getproduct($conds,$page,$pageSize);
        $count = D("Product")->getproductCount($conds);
        $catelsit = D("Category")->select();

        $res  =  new \Think\Page($count,$pageSize);
        $pageres = $res->show();
        $this->assign('pageres',$pageres);
        $this->assign('product',$product);
        $this->assign('catelsit',$catelsit);
        $this->display();
    }

    public function add(){
        $cate = D("Category")->select();
        if($_POST) {
            if (!isset($_POST['pname']) || !$_POST['pname']) {
                return show(0, '产品名称不能为空');
            }
            if (!isset($_POST['code']) || !$_POST['code']) {
                return show(0, '货号不能为空');
            }
            if (!isset($_POST['catid']) || !$_POST['catid']) {
                return show(0, '请选择分类');
            }
            if (!isset($_POST['material']) || !$_POST['material']) {
                return show(0, '请选择发丝材质');
            }
            if (!isset($_POST['tech']) || !$_POST['tech']) {
                return show(0, '请选择制作工艺');
            }
            if (!isset($_POST['model']) || !$_POST['model']) {
                return show(0, '请选择发型');
            }
            if ($_POST['id']) {
                return $this->save($_POST);
            }
            $cid = D("Product")->insert($_POST);
            if ($cid) {
                return show(1, '新增成功');
            }
            return show(0, '新增失败');
        }else{
            $material = C("MATERIAL");
            $tech = C("TECH");
            $model = C("MODEL");
            $this->assign('cate',$cate);
            $this->assign('model',$model);
            $this->assign('material',$material);
            $this->assign('tech',$tech);
            $this->display();
        }
    }

    public function setStatus() {
        try {
            if ($_POST) {
                $id = $_POST['id'];
                $status = $_POST['status'];
                if (!$id) {
                    return show(0, 'ID不存在');
                }
                $res = D("Product")->updateStatusById($id, $status);
                if ($res) {
                    return show(1, '操作成功');
                } else {
                    return show(0, '操作失败');
                }
            }
            return show(0, '没有提交的内容');
        }catch(Exception $e) {
            return show(0, $e->getMessage());
        }
    }

    public function edit(){
        $id = $_GET['id'];
        $pro = D("Product") -> find($id);
        $cate = D("Category")->select();


        $material = C("MATERIAL");
        $tech = C("TECH");
        $model = C("MODEL");
        $this->assign('model',$model);
        $this->assign('material',$material);
        $this->assign('tech',$tech);
        $this->assign('cate',$cate);
        $this->assign('pro',$pro);
        $this->display();
    }

    public function save($data){
        $id = $data['id'];
        unset($data['id']);
        try {
            $id = D("Product")->updateById($id, $data);
            if($id === false) {
                return show(0,'更新失败');
            }
            return show(1,'更新成功');
        }catch(Exception $e) {
            return show(0,$e->getMessage());
        }
    }

}